import { Component, OnInit, Input } from '@angular/core';
import { AppGlobalService } from '../../shared/service/app-global.service';
import { EventService } from '../../shared/service/event.service';
import { HttpService } from '../../shared/service/http.service';
import { RemarksContentComponent } from "../remarks-content/remarks-content.component";
import { AlertController } from '@ionic/angular';
import { ModalController } from "@ionic/angular";
@Component({
  selector: 'app-inquiry-page-modal',
  templateUrl: './inquiry-page-modal.component.html',
  styleUrls: ['./inquiry-page-modal.component.scss'],
})
export class InquiryPageModalComponent implements OnInit {

  constructor(
    private alertController: AlertController,
    private http: HttpService,
    private appGlobalService: AppGlobalService,
    private eventService: EventService,
    public modalController: ModalController
  ) { }
  @Input() tagData: any;
  pageSize: 0;
  ngOnInit() {
    // if (this.appGlobalService.isShowInquiryPageModal) {
    //   this.appGlobalService.isShowInquiryPageModal = false;
    this.presentAlertConfirm();
    // this.presentModal('remarks', this.getOpt());
    // }
  }
  async presentAlertConfirm() {
    try {
      const alert = await this.alertController.create({
        cssClass: 'my-custom-class',
        header: '',
        // tslint:disable-next-line:max-line-length
        message: '<span "height:2.75rem;font-size:1rem;font-weight:bold;color:rgba(39,40,46,1);">' + this.tagData.data.title + '</span>',
        buttons: [
          {
            text: this.tagData.data.cancel,
            role: 'cancel',
            handler: (blah) => {
              this.appGlobalService.isShowInquiryPageModal = true;
            }
          }, {
            text: this.tagData.data.button.label,
            cssClass: 'custom-ok-btn',
            handler: () => {
              this.appGlobalService.isShowInquiryPageModal = true;
              if (this.tagData.data.button.submit !== undefined) {
                const params = {
                  content: this.tagData.data.button.submit.content,
                  id: this.tagData.data.button.submit.id,
                  sessionId: localStorage.getItem('sessionId')
                };
                this.http.post('application/submit', params, res => {
                  // 请求数据成功
                  if (res.success === true) {
                    this.appGlobalService.showToast(res.message, 2000, 'middle');
                    setTimeout(() => {
                      this.eventService.eventEmit.emit('reloadCurrentPage', '1');
                      localStorage.setItem('needRefresh', 'true');
                    }, 1500);
                  } else {
                    // 请求数据失败
                    this.appGlobalService.showToast(res.message, 2000, 'middle');
                  }
                });
              }
            }
          }
        ]
      });
      await alert.present();
    } catch (error) {
      console.log('error------>' + JSON.stringify(error));
    }
  }

  // 业务备注->填写备注弹框
  async presentModal(value, opt) {
    opt.submit.content.remark_class = "1";
    opt.submit.content.remark_text = "";
    opt.submit.content.remark_type = "0";
    const remarkIcon = {
      iconName: "assets/img/List_operation_icon_bz@3x.png",
      formData: {
        button: {
          label: "确定",
          submit: opt.submit
        },
        textarea: opt.textarea
      }
    };
    const modal = await this.modalController.create({
      showBackdrop: true,
      cssClass: value,
      component: RemarksContentComponent,
      componentProps: { tagData: { data: { data: [] } }, item: remarkIcon, type: value, pageSize: this.pageSize }
    });
    await modal.present();
    // 监听销毁的事件
    // const modalData = await modal.onDidDismiss(); // 获取关闭传回的值
    modal.onDidDismiss().then((modalData) => {
      this.appGlobalService.isShowInquiryPageModal = true;
      console.log(modalData);
    });
    // console.log(modalData);
  }

  getOpt() {
    return {
      submit: {
        id: "0643174f-a7b6-43d4-92a9-cf95f6af578c",
        content: {
          opportunity_id: "5df0bb3c59e1da0001411c15",
          remark: "",
          remark_class: '1', remark_text: '', remark_type: '0'
        }
      },
      textarea: {
        formKey: "remark",
        minLength: "0",
        placeholder: "请填写备注",
        maxLength: "300"
      },
      style: "popup",
      label: "添加"
    };
  }

}
